package com.hundsun.fcp.common;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;

import com.hundsun.fcp.R;
import com.hundsun.fcp.common.AppointFiled.SharePlatformType;
import com.hundsun.fcp.common.utils.StringUtils;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.bean.SocializeEntity;
import com.umeng.socialize.bean.StatusCode;
import com.umeng.socialize.controller.UMServiceFactory;
import com.umeng.socialize.controller.UMSocialService;
import com.umeng.socialize.controller.listener.SocializeListeners.SnsPostListener;
import com.umeng.socialize.media.QQShareContent;
import com.umeng.socialize.media.QZoneShareContent;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.sso.QZoneSsoHandler;
import com.umeng.socialize.sso.SinaSsoHandler;
import com.umeng.socialize.sso.TencentWBSsoHandler;
import com.umeng.socialize.sso.UMQQSsoHandler;
import com.umeng.socialize.sso.UMSsoHandler;
import com.umeng.socialize.weixin.controller.UMWXHandler;
import com.umeng.socialize.weixin.media.CircleShareContent;
import com.umeng.socialize.weixin.media.WeiXinShareContent;

public class ShareWindow {
	public static final String DESCRIPTOR = "com.umeng.share";
	private final UMSocialService mController = UMServiceFactory.getUMSocialService(DESCRIPTOR);
	Context mContext ;
	private String mShareUrl ;
	private String mTitle ="E比价" ;
	
	
	public ShareWindow(Context pContext){
		mContext = pContext ; 
		setController();
	}
	

	private void setController() {
		// 添加新浪SSO授权
        mController.getConfig().setSsoHandler(new SinaSsoHandler());
        // 添加腾讯微博SSO授权
        mController.getConfig().setSsoHandler(new TencentWBSsoHandler());
        addQQQZonePlatform() ;
        addWXPlatform() ;
        mController.getConfig().setPlatforms(SHARE_MEDIA.WEIXIN, SHARE_MEDIA.WEIXIN_CIRCLE,
                SHARE_MEDIA.QQ,SHARE_MEDIA.QZONE, SHARE_MEDIA.SINA,SHARE_MEDIA.TENCENT);
	}
	
	/**
     * @功能描述 : 添加QQ平台支持 QQ分享的内容， 包含四种类型， 即单纯的文字、图片、音乐、视频. 参数说明 : title, summary,
     *       image url中必须至少设置一个, targetUrl必须设置,网页地址必须以"http://"开头 . title :
     *       要分享标题 summary : 要分享的文字概述 image url : 图片地址 [以上三个参数至少填写一个] targetUrl
     *       : 用户点击该分享时跳转到的目标地址 [必填] ( 若不填写则默认设置为友盟主页 )
     * @return
     */
    private void addQQQZonePlatform() {
        String appId = "1104239389";
        String appKey = "pzWZrmTVYqiPWJnu";
        // 添加QQ支持, 并且设置QQ分享内容的target url
        UMQQSsoHandler qqSsoHandler = new UMQQSsoHandler((Activity)mContext,appId, appKey);
        qqSsoHandler.setTargetUrl(getShareUrl());
        qqSsoHandler.addToSocialSDK();

        // 添加QZone平台
        QZoneSsoHandler qZoneSsoHandler = new QZoneSsoHandler((Activity)mContext, appId, appKey);
        qZoneSsoHandler.setTargetUrl(getShareUrl()) ;
        qZoneSsoHandler.addToSocialSDK();
    }
    
    /**
     * @功能描述 : 添加微信平台分享
     * @return
     */
    private void addWXPlatform() {
        // 注意：在微信授权的时候，必须传递appSecret
        // wx967daebe835fbeac是你在微信开发平台注册应用的AppID, 这里需要替换成你注册的AppID
        String appId = "wx205237639f3fc78a";
        String appSecret = "f81a8f274b55d12594f5fa50a7391ea4";
        // 添加微信平台
        UMWXHandler wxHandler = new UMWXHandler((Activity)mContext, appId, appSecret);
        wxHandler.setTargetUrl(getShareUrl()) ;
        wxHandler.addToSocialSDK();

        // 支持微信朋友圈
        UMWXHandler wxCircleHandler = new UMWXHandler((Activity)mContext, appId, appSecret);
        wxCircleHandler.setTargetUrl(getShareUrl()) ;
        wxCircleHandler.setToCircle(true);
        wxCircleHandler.addToSocialSDK();
    }
    
    /**
     * 产品名称，预期收益率：6%，起售时间：2015-01-01，起购金额：5万(人民币），发行银行：招商银行，#e比价# 
     * 设置分享内容</br>
     */
    private void setShareContent(String shareContent,String imageUrl) {
    	// 设置分享内容
    	mController.setShareContent(shareContent+"  "+getShareUrl());
    	if(!StringUtils.isEmpty(imageUrl)){
	    	// 设置分享图片, 参数2为图片的url地址
	    	mController.setShareMedia(new UMImage(mContext, imageUrl));
    	}else{
    		mController.setShareMedia(new UMImage(mContext, R.drawable.ic_launcher)) ;
    	}
    	mController.setAppWebSite(getShareUrl()) ;
    	setPlatformShareContent(shareContent, imageUrl) ;
    	
    }
    
    /**
     * 产品名称，预期收益率：6%，起售时间：2015-01-01，起购金额：5万(人民币），发行银行：招商银行，#e比价# 
     * 
     * 设置分享内容</br>
     */
    private void setShareContent(String shareContent,int imageResId) {
    	// 设置分享内容
    	mController.setShareContent(shareContent+"  "+getShareUrl());
    	// 设置分享图片, 参数2为图片的url地址
    	mController.setShareMedia(new UMImage(mContext, imageResId));
    	mController.setAppWebSite(getShareUrl()) ;
    	
    	setPlatformShareContent(shareContent, imageResId) ;  	 
    }
    
    
    private void setPlatformShareContent(String shareContent,Object imageResId){
    	UMImage urlImage = null ;
    	if(imageResId!=null&&imageResId.toString().length()>0){
    		if(imageResId instanceof String){
	    		urlImage = new UMImage(mContext, (String)imageResId) ;
	    	}else{
	    		urlImage = new UMImage(mContext, (Integer)imageResId) ;
	    	}
    	}else{
    		urlImage = new UMImage(mContext, R.drawable.ic_launcher) ;
    	}
    	
    	WeiXinShareContent weixinContent = new WeiXinShareContent();
        weixinContent.setShareContent(shareContent);
        weixinContent.setTitle(getTitle());
        weixinContent.setTargetUrl(getShareUrl());
        weixinContent.setShareMedia(urlImage);
        mController.setShareMedia(weixinContent);

        // 设置朋友圈分享的内容
        CircleShareContent circleMedia = new CircleShareContent();
        circleMedia.setShareContent(shareContent);
        circleMedia.setTitle(getTitle()+" "+shareContent);
        circleMedia.setShareMedia(urlImage); 
        circleMedia.setTargetUrl(getShareUrl());
        mController.setShareMedia(circleMedia);
        
        // 设置QQ空间分享内容
        QZoneShareContent qzone = new QZoneShareContent();
        qzone.setShareContent(shareContent);
        qzone.setTargetUrl(getShareUrl());
        qzone.setTitle(getTitle());
        qzone.setShareMedia(urlImage); 
        mController.setShareMedia(qzone);
        
        QQShareContent qqShareContent = new QQShareContent();
        qqShareContent.setShareContent(shareContent);
        qqShareContent.setTitle(getTitle());
        qqShareContent.setShareMedia(urlImage);
        qqShareContent.setTargetUrl(getShareUrl());
        mController.setShareMedia(qqShareContent);
    }
    
	/**
	 * 分享
	 * @param shareContent 分享内容
	 * @param imageUrl 分享图片地址
	 */
    public void share(String shareContent,String imageUrl){
    	setShareContent(shareContent, imageUrl) ;
        mController.openShare((Activity)mContext, snsPostListener) ;
    }
    
    
    /**
     * 针对单个平台分享
     * @param shareContent
     * @param platformType
     */
    public void sharePlatform(String shareContent,int platformType){
    	setShareUrl("http://www.hsebj.com/") ;
    	SHARE_MEDIA _Platform = SHARE_MEDIA.QQ ;
    	switch (platformType) {
		case SharePlatformType.WEIXINCHAR:
			_Platform = SHARE_MEDIA.WEIXIN ;
			break;
		case SharePlatformType.WEIXINCIRCLE:
			_Platform = SHARE_MEDIA.WEIXIN_CIRCLE ;
			break;
		case SharePlatformType.QQ:
			_Platform = SHARE_MEDIA.QQ ;
			break;
		case SharePlatformType.SINA:
			_Platform = SHARE_MEDIA.SINA ;
			break;

		default:
			break;
		}
    	setShareContent(shareContent,null) ;
    	performShare(_Platform) ;
    	
    }
    
    private void performShare(SHARE_MEDIA platform) {
    	
        mController.postShare(mContext, platform, new SnsPostListener() {
            @Override
            public void onStart() {}
            @Override
            public void onComplete(SHARE_MEDIA platform, int eCode, SocializeEntity entity) {
                String showText = platform.toString();
                if (eCode == StatusCode.ST_CODE_SUCCESSED) {
                    showText += "平台分享成功";
                } else {
                    showText += "平台分享失败";
                }
                Redirect.makeToast(mContext, showText) ;
            }
        });
    }
    
    /**
     * 分享
     * @param shareContent 分享内容
     * @param imageResid 分享资源图片
     */
    public void share(String shareContent,int imageResid){
    	setShareContent(shareContent, imageResid) ;
        mController.openShare((Activity)mContext, snsPostListener) ;
    }
    
    public String getTitle() {
		return mTitle;
	}

	public void setTitle(String mTitle) {
		if(!StringUtils.isEmpty(mTitle)){
			this.mTitle = mTitle;
		}
	}

	private SnsPostListener snsPostListener = new SnsPostListener() {
		@Override
		public void onStart() {}
		@Override
		public void onComplete(SHARE_MEDIA platform, int eCode, SocializeEntity entity) {
			if (eCode == 200) {
				Redirect.makeToast(mContext, "分享成功.");
            } else {
                 String eMsg = "";
                 if (eCode == -101){
                     eMsg = "没有授权";
                 } 
                 //Redirect.makeToast(mContext, platform.name()+"分享失败[" + eMsg + "] ");
            }
			
			mController.getConfig().cleanListeners() ;
		}
	};
    
	
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
	    /** 使用SSO授权必须添加如下代码 */
	    UMSsoHandler ssoHandler = mController.getConfig().getSsoHandler(
	            requestCode);
	    if (ssoHandler != null) {
	        ssoHandler.authorizeCallBack(requestCode, resultCode, data);
	    }
	}

	public String getShareUrl() {
		return mShareUrl;
	}

	public void setShareUrl(String mShareUrl) {
		this.mShareUrl = mShareUrl;
	}
}
